<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html >
<html lang="en">
<head>
<%@include file="../../../common/includeBaseSet.jsp" %>
<%@include file="../../../common/includeSystemSet.jsp" %>
<%@include file="../../../common/operation_forbid.jsp" %>
<%@include file="../../../common/jsVersion.jsp" %>
</head>
<body>
	<div class="page-content">
		    <div class="col-xs-12 panel panel-success" style="height:95%"> 
	         	 <div class="col-xs-6" style="width:100%">
	         	     <div class="form-group" style="text-align:center;margin-top: 3%">
                        <label class="col-xs-3 control-label">问卷类型：</label>
                        <select name="questionnaireType" id="questionnaireType" class="form-control" style="width:65%" onchange="getModel()">
                             <c:choose>
                                 <c:when test="${questionnaireType != null && questionnaireType != ''}">
                                     <c:forEach items="${questionnaireType}" var="list">
                                         <option value="${list.no}">${list.des}</option>
                                     </c:forEach>
                                 </c:when>
                             </c:choose>
                        </select>
                     </div>
	         	 	 <div id="model" class="form-group" style="text-align:center;margin-top: 3%">
						<label class="col-xs-3 control-label">问卷模板：</label>
	                    <select name="modelId" id="modelId" class="form-control" style="width:65%" onchange="getName()">
	                    	 <!-- 问卷模板下拉框 -->
						</select>
				     </div>
	         	     <div class="form-group" style="text-align:center;margin-top: 3%">
						<label class="col-xs-3 control-label">问卷名称：</label>
	                    <input class="form-control" style="width:65%" type="text" name="name" id="name" maxlength="20"
	                    	placeholder="问卷名称不得超过20个字" onfocus="this.placeholder=''" onblur="this.placeholder='问卷名称不得超过20个字'"/>
				     </div>
				     <div class="form-group" style="text-align:center;margin-top: 3%">
						<label class="col-xs-3 control-label">问卷描述：</label>
	                    <textarea class="form-control" style="width:65%;resize:none;" rows="4" maxlength="140" name="describe" id="describe" 
	                    	placeholder="问卷描述不得超过140个字" onfocus="this.placeholder=''" onblur="this.placeholder='问卷描述不得超过140个字'"></textarea>
				     </div>
				     <div class="form-group" style="text-align:center;margin-top: 3%">
						<label class="col-xs-3 control-label">绑定项目：</label>
	                    <select name="projectId" id="projectId" class="form-control" style="width:65%">
						     <c:forEach items="${projectList}" var="list">
							     <option value="${list.id}">${list.projectName}</option>
						     </c:forEach>
						</select>
				     </div>
				     <div class="form-group" style="margin-top: 3%">
						<label class="col-xs-3 control-label" style="padding-left:7%;">是否启用：</label>
	                    <input type="radio" value="1" name="isValid">启用&nbsp;&nbsp;&nbsp;
	                    <input type="radio" value="0" name="isValid" checked="true">不启用
				     </div>
				     <div class="form-group" style="margin-top: 3%">
						<label class="col-xs-3 " style="padding-left:7%; width:99%;">创建人：${userName }</label>
				     </div>
				     <div class="form-group" style="margin-top: 3%">
						<label class="col-xs-3 " style="padding-left:7%; width:99%;">创建时间：${createTime }</label>
				     </div>
				     <div id="btnDiv" class="form-group" style="margin-top:13px;text-align:center;">
						<button type="button" onclick="add()" class="btn btn-default">确定</button>
						<button type="button" onclick="callback()" class="btn btn-link">取消</button>
				     </div>
				 </div>
		    </div>
	</div>	
</body>
<script type="text/javascript">
	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	var btn = false; //判断是否已提交，默认未提交
	$(function(){
		getModel();
	});
	
	//根据类型判断时候需要显示模板
    function getModel(){
        var type = $("#questionnaireType").val();
        
        $.ajax({
			type: 'post',
            url: '${jypath}/customer/projectQuestionnaire/getModel',
            data:{
            	"questionnaireType": type,
            },
            dataType: 'json',
            success: function(data){
            	if(data.res == "1"){
            		var html = "";
            		if(data.obj == null){
            			html = "<option value=''>没有可以使用的模板</option>";
            			$("#modelId").html(html);
            			return false;
            		}
            		html = "<option value=''>请选择模板</option>";
            		for(var i = 0; i < data.obj.length; i++){
            			var info = data.obj[i];
            			html += "<option value='"+info.id+"'>"+info.name+"</option>";
            		}
            		$("#modelId").html(html);
            	}
            }
        });
    }
	//根据模板自动带出问卷名称
	function getName(){
		var modelId = $("#modelId").val();
		if(modelId != ""){
			var name = $("#modelId").children('option:selected').text();
			$("#name").val(name);
		}else{
			$("#name").val("");
		}
	}
	//新增问卷
	function add(){
		var name = $("#name").val();
		if(name == ""){
			layer.tips("请填写问卷名称", "#name", {tips: 3});
			return false;
		}
		var modelId = $("#modelId").val();
		if(modelId == ""){
			layer.tips("请选择问卷模板", "#modelId", {tips: 3});
			return false;
		}
		var describe = $("#describe").val();
		var isValid = $('input:radio[name="isValid"]:checked').val();
		
		if(btn){
			layer.msg("正在保存中，请勿重复提交");
			return false;
		}
		btn = true; //将按钮状态修改为已提交
		
		//设置加载层
		var loadIndex = layer.load(1, {
	        shade: [0.1,'#fff'] //0.1透明度的白色背景
	    });
		$.ajax({
			type: 'post',
            url: '${jypath}/customer/projectQuestionnaire/add',
            data:{
            	"type": $("#questionnaireType").val(),
            	"modelId": $("#modelId").val(),
                "name": name,
                "questionnaireDescribe": describe,
                "projectId": $("#projectId").val(),
                "isValid": isValid
            },
            dataType: 'json',
            success: function(data){
            	btn = false; //执行完成后，将按钮状态修改回未提交
            	layer.closeAll('loading'); //关闭加载层
            	if(data.res == "1"){
            		layer.msg("保存成功");
            		parent.loadProjectQuestionnaire();
            		//延时执行执行回调方法并关闭窗口
                    setTimeout("callback()", 2000);
            	}else{
            		layer.msg(data.resMsg);
            	}
            }
		});
	}
	//执行回调方法并关闭窗口
	function callback(){
		parent.layer.close(index);
	}
	
</script>
</html>